#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>

#define MAP_SIZE    (4L * 1024L) /* 1 PAGE */
#define RAMFILE     "/mnt/ramdisk/ramfile"
#define OFFSET      (4L * 1024L * 1024L) /* 4 MiB offset */

int main()
{
    /* OPEN RAMFILE */
    int fd = open(RAMFILE, O_RDWR);
    if (fd < 0) {
        perror("open");
        return 1;
    }

    /* MMAP MEM */
    void *base = mmap(NULL, MAP_SIZE,
            PROT_READ | PROT_WRITE, MAP_SHARED,
            fd, OFFSET);
    if (base == MAP_FAILED){
        perror("mmap");
        return 1;
    }

    /* WRITE DATA */
    const char *write_data = "Hello from ramfile!.\n";
    size_t write_len = strlen(write_data) + 1;

    memcpy(base, write_data, write_len);

    /* UNMAP */
    munmap(base, MAP_SIZE);
    
    return 0;
}
